package com.hkts.management.unattended.poundroom.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.management.unattended.poundroom.entity.YbMutualInventory;
import com.hkts.management.unattended.poundroom.entity.YbMutualPovouch;
import com.hkts.management.unattended.poundroom.mapper.YbMutualPovouchMapper;
import com.hkts.management.unattended.poundroom.service.IYbMutualPovouchService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zy
 * @since 2021-10-13
 */
@Service
@Transactional
public class YbMutualPovouchServiceImpl extends ServiceImpl<YbMutualPovouchMapper, YbMutualPovouch> implements IYbMutualPovouchService {


    @Autowired
    private YbMutualPovouchMapper ybMutualPovouchMapper;

    @Override
    public IPage<YbMutualPovouch> selectPage(Page<YbMutualPovouch> page, YbMutualPovouch ybMutualPovouch) {
        QueryWrapper<YbMutualPovouch> query = Wrappers.query();
        return ybMutualPovouchMapper.selectPage(page, query);
    }
    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<YbMutualPovouch> getEntityWrapper(QueryWrapper<YbMutualPovouch> entityWrapper, YbMutualPovouch ybMutualPovouch) {
        //条件拼接
        if (StringUtils.isNotBlank(ybMutualPovouch.getcPOID())) {
            entityWrapper.like(YbMutualPovouch.CPOID, ybMutualPovouch.getcPOID());
        }
        if (StringUtils.isNotBlank(ybMutualPovouch.getcPTCode())) {
            entityWrapper.like(YbMutualPovouch.CPTCODE, ybMutualPovouch.getcPTCode());
        }
        if (StringUtils.isNotBlank(ybMutualPovouch.getcDepCode())) {
            entityWrapper.like(YbMutualPovouch.CDEPCODE, ybMutualPovouch.getcDepCode());
        }
        if (StringUtils.isNotBlank(ybMutualPovouch.getcDepName())) {
            entityWrapper.like(YbMutualPovouch.CDEPNAME, ybMutualPovouch.getcDepName());
        }
        if (StringUtils.isNotBlank(ybMutualPovouch.getcPersonCode())) {
            entityWrapper.like(YbMutualPovouch.CPERSONCODE, ybMutualPovouch.getcPersonCode());
        }
        if (StringUtils.isNotBlank(ybMutualPovouch.getcPersonName())) {
            entityWrapper.like(YbMutualPovouch.CPERSONNAME, ybMutualPovouch.getcPersonName());
        }
        if (StringUtils.isNotBlank(ybMutualPovouch.getcVenCode())) {
            entityWrapper.like(YbMutualPovouch.CVENCODE, ybMutualPovouch.getcVenCode());
        }
        if (StringUtils.isNotBlank(ybMutualPovouch.getcVenAbbName())) {
            entityWrapper.like(YbMutualPovouch.CVENABBNAME, ybMutualPovouch.getcVenAbbName());
        }
        if (StringUtils.isNotBlank(ybMutualPovouch.getcInvCode())) {
            entityWrapper.like(YbMutualPovouch.CINVCODE, ybMutualPovouch.getcInvCode());
        }
        if (StringUtils.isNotBlank(ybMutualPovouch.getcInvName())) {
            entityWrapper.like(YbMutualPovouch.CINVNAME, ybMutualPovouch.getcInvName());
        }
        if (StringUtils.isNotBlank(ybMutualPovouch.getcInvStd())) {
            entityWrapper.like(YbMutualPovouch.CINVSTD, ybMutualPovouch.getcInvStd());
        }
        if (StringUtils.isNotBlank(ybMutualPovouch.getcComUnitName())) {
            entityWrapper.like(YbMutualPovouch.CCOMUNITNAME, ybMutualPovouch.getcComUnitName());
        }
        return entityWrapper;
    }

    @Override
    public List<YbMutualPovouch> selectList(String name) {
        QueryWrapper<YbMutualPovouch> query = Wrappers.query();
        YbMutualPovouch ybMutualPovouch = new YbMutualPovouch();
        ybMutualPovouch.setcInvName(name);
        QueryWrapper<YbMutualPovouch> cInvNameWrapper = getEntityWrapper(query, ybMutualPovouch);
        List<YbMutualPovouch> ybMutualPovouchesBycInvName = ybMutualPovouchMapper.selectList(cInvNameWrapper);

        if (ybMutualPovouchesBycInvName != null && ybMutualPovouchesBycInvName.size() > 0){
            return ybMutualPovouchesBycInvName;
        }else {
            ybMutualPovouch.setcVenAbbName(name);
            QueryWrapper<YbMutualPovouch> VenAbbNameWrapper = getEntityWrapper(query, ybMutualPovouch);
            List<YbMutualPovouch> ybMutualPovouchesByVenAbbName = ybMutualPovouchMapper.selectList(VenAbbNameWrapper);
            if (ybMutualPovouchesByVenAbbName.size() > 0){
                return ybMutualPovouchesByVenAbbName;
            }else {
                return null;
            }
        }
    }
    public LocalDateTime getLastUpdateTime(){
        QueryWrapper<YbMutualPovouch> query = Wrappers.query();
        query.lambda().orderByDesc(YbMutualPovouch::getDoptTime);
        query.last("limit 1");
        List<YbMutualPovouch> ybMutualCompanies=ybMutualPovouchMapper.selectList(query);
        return ybMutualCompanies.size()>0?ybMutualCompanies.get(0).getDoptTime():null;
    }
    public void mysqlInsertOrUpdateBath(List<YbMutualPovouch> ybMutualCompanies){
        ybMutualPovouchMapper.mysqlInsertOrUpdateBath(ybMutualCompanies);
    }
    @Override
    public YbMutualPovouch getBycPOID(String code){
        QueryWrapper<YbMutualPovouch> query = Wrappers.query();
        query.lambda().eq(YbMutualPovouch::getcPOID,code);
        return ybMutualPovouchMapper.selectOne(query);
    }


    @Override
    public List<Map<String,Object>> selectListByName(String name) {
        QueryWrapper<YbMutualPovouch> query = Wrappers.query();
        query.lambda().like(YbMutualPovouch::getcInvName,name).or().like(YbMutualPovouch::getcVenAbbName,name);
        List<YbMutualPovouch> ybMutualPovouches = ybMutualPovouchMapper.selectList(query);
        List<Map<String,Object>> collect = ybMutualPovouches.stream().map(this::assemblyybMutualPovouchData).collect(Collectors.toList());
        return collect;
    }

    private Map<String,Object> assemblyybMutualPovouchData(YbMutualPovouch ybMutualPovouches) {
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("orderCode",ybMutualPovouches.getcPOID());
        resultMap.put("providerOrClient",ybMutualPovouches.getcVenAbbName());
        resultMap.put("cInvName",ybMutualPovouches.getcInvName());
        resultMap.put("estimatedShipDate",ybMutualPovouches.getDmodifyTime());
        resultMap.put("model",ybMutualPovouches.getcInvStd());
        resultMap.put("orderQuantity",ybMutualPovouches.getiQuantity());
        resultMap.put("noOrAddUp","");
        return resultMap;
    }

}
